package ModelLayer;
import java.util.ArrayList;


/**
 * a container for the class product
 * 
 * @author Team Black
 * @version 1.0
 */
public class ProductLibrary
{
    // instance variables - replace the example below with your own
   private Product myProduct;
   private static ProductLibrary Instance;
   private ArrayList<Product>ProductList;
    /**
     * Constructor for objects of class ProductLibrary
     */
    private ProductLibrary()
    {
        ProductList = new ArrayList<Product>();
    }
    
     /**
     * makes the container singleton
     */
     public static ProductLibrary getInstance()
    {
        if(Instance == null)
           {
               Instance = new ProductLibrary();
           }
       return Instance;
    }
    
      /**
     * adds a product to the container
     */
    public void addProduct(Product _product)
    {
        ProductList.add(_product);
    }
    /**
     * removes a product from the list of products
     */
    public void deleteProdukt(Product _product)
    {
        ProductList.remove(_product);
    }
    /**
     * searh for a priduct, by the Name
     */
    public Product searchById(int _id)
    {
        Product retProduct = null;
        int index = 0;
        boolean found = false;
        while(index<ProductList.size() && !found)
        {
            Product currProduct = ProductList.get(index);
            if(currProduct.getId() == _id)
            {
                retProduct = currProduct;
                found =true;
            }
            else
            {
                index++;
            }
        }
        return retProduct;
    }
    /**
     * search for a product, by the id
     */
    public Product searchByName(String _name)
    {
        Product retProduct = null;
        int index = 0;
        boolean found = false;
        while(index<ProductList.size() && !found)
        {
            Product currProduct = ProductList.get(index);
            if(currProduct.getName().equalsIgnoreCase(_name))
            {
                retProduct = currProduct;
                found =true;
            }
            else
            {
                index++;
            }
        }
        return retProduct;
    }


}
